/**
 * 
 */
package com.pxingg.model.pubsub;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.pxingg.Global;
import com.pxingg.log.LogConfig;

/**
 * @author pxingg
 * 
 */
public class SubscriberTest
{
    /**
     * The log of this class
     */
    static Log log = LogFactory.getLog(SubscriberTest.class);

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        // 打开日志开关
        LogConfig.init();
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
    }

    /**
     * 测试时，订阅者和消息的名字
     */
    String name = "com.pxingg.SubscriberTest";

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.Subscriber#process(java.lang.String, com.pxingg.model.pubsub.Message)}.
     */
    @Test
    public final void testProcess()
    {
        final Subscriber subscriber = new Subscriber(name);
        final Message message = new Message(name);
        subscriber.process(name, message);
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.Subscriber#subscribe(java.lang.String)}.
     */
    @Test
    public final void testSubscribe()
    {
        Global.switchCenter.removeAllSubscriber();
        final Subscriber subscriber = new Subscriber(name);
        subscriber.subscribe(name);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));
        Global.switchCenter.removeAllSubscriber();
        Assert.assertFalse(Global.switchCenter.toString().contains(" = " + name));
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.Subscriber#Subscriber(java.lang.String)}.<br>
     * Test method for {@link com.pxingg.model.pubsub.Subscriber#getName()}.
     */
    @Test
    public final void testSubscriber()
    {
        final Subscriber subscriber = new Subscriber(name);
        Assert.assertEquals(name, subscriber.getName());
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.Subscriber#unsubscribe(java.lang.String)}.
     */
    @Test
    public final void testUnsubscribe()
    {
        Global.switchCenter.removeAllSubscriber();
        final Subscriber subscriber = new Subscriber(name);
        subscriber.subscribe(name);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

        // 从一个不存在的消息中删除，对操作不影响
        subscriber.unsubscribe(name + "notExist");
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

        // 取消订阅的消息
        subscriber.unsubscribe(name);
        Assert.assertFalse(Global.switchCenter.toString().contains(" = " + name));
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.Subscriber#unsubscribeAll()}.
     */
    @Test
    public final void testUnsubscribeAll()
    {
        Global.switchCenter.removeAllSubscriber();
        final Subscriber subscriber = new Subscriber(name);
        subscriber.subscribe(name + "0").subscribe(name + "1").subscribe(name + "2");

        Assert.assertTrue(Global.switchCenter.toString().contains(name + "0"));
        Assert.assertTrue(Global.switchCenter.toString().contains(name + "1"));
        Assert.assertTrue(Global.switchCenter.toString().contains(name + "2"));

        Global.switchCenter.removeAllSubscriber();
        Assert.assertFalse(Global.switchCenter.toString().contains(name + "0"));
        Assert.assertFalse(Global.switchCenter.toString().contains(name + "1"));
        Assert.assertFalse(Global.switchCenter.toString().contains(name + "2"));
    }
}
